﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 

public partial class management_Produt_Category_List : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitTree(0, null);
        }
    }

    //绑定树型菜单
    private void InitTree(int ParentID, TreeNode pNode)
    {
        int total = 0; DataTable dt = BLL.Product_Category.Categorys_Get(0, int.MaxValue, 0, out total);
        DataView dvTree = new DataView(dt);
        dvTree.RowFilter = "FId= " + ParentID;

        foreach (DataRowView Row in dvTree)
        {
            TreeNode Node = new TreeNode();
            if (pNode == null)
            {    //添加根节点
                Node.Text = Row["Title"].ToString() + "<font color='#003399'>·修改</font>";
                Node.Value = Row["Id"].ToString();
                TreeCategory.Nodes.Add(Node);
                Node.Expanded = false;
                InitTree(Int32.Parse(Row["Id"].ToString()), Node);    //再次递归
            }
            else
            {   //̀添加当前节点的子节点
                Node.Text = Row["Title"].ToString() + "<font color='#003399'>·修改</font>";
                Node.Value = Row["Id"].ToString();
                pNode.ChildNodes.Add(Node);
                Node.Expanded = false;
                InitTree(Int32.Parse(Row["Id"].ToString()), Node);     //再次递归
            }
        }
    }

    //筛选删除
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        foreach (TreeNode t in TreeCategory.CheckedNodes)
        {
            int id = Convert.ToInt32(t.Value); 
            if (BLL.Product.GetModelByCid(id.ToString()) != null) 
            {
                Page.RegisterStartupScript("back", "<script   languge='javascript'>alert('请先删除分类内的产品！');</script>");
                return;
            }
            else
            {
                BLL.Product_Category.Delete(id);
            }
        }
        Response.Redirect("Category_List.aspx");
    }
     

    //修改节点
    protected void TreeCategory_SelectedNodeChanged(object sender, EventArgs e)
    {
        string id = TreeCategory.SelectedNode.Value;
        Response.Redirect("Category_Edit.aspx?p=" + id);
    }
}
